package com.ywy.controller.admin;

import com.ywy.entity.Comment;
import com.ywy.service.CommentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员用户评论Controller
 *
 * @author ywy
 * @date 2022/9/7 14:19
 */
@Controller
@RequestMapping("admin/comment")
public class AdminCommentController {
    @Resource
    private CommentService commentService;

    /**
     * 分页查询评论信息
     *
     * @param comment
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    @RequiresPermissions(value = {"COMMENT_QUERY"})
    public Map<String, Object> list(Comment comment, Integer page, Integer limit) {
        List<Comment> commentList = commentService.list(comment, page, limit);
        Long count = commentService.getTotal(comment);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("count", count);
        resultMap.put("data", commentList);
        return resultMap;
    }

    /**
     * 修改评论状态
     *
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping("updateStatus")
    @RequiresPermissions(value = {"COMMENT_EDIT"})
    public Map<String, Object> updateStatus(Comment comment) {
        Comment oldComment = commentService.getById(comment.getId());
        if (comment.getStatus() == 1) {
            oldComment.setStatus(1);
        } else {
            oldComment.setStatus(2);
        }
        commentService.save(oldComment);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 根据id删除评论
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    @RequiresPermissions(value = {"COMMENT_DELETE"})
    public Map<String, Object> delete(Integer id) {
        // 删除评论信息
        commentService.delete(id);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 批量删除资源评论
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteBatch")
    @RequiresPermissions(value = {"COMMENT_DELETE"})
    public Map<String, Object> deleteBatch(String ids) {
        String[] idsStr = ids.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            // 删除评论信息
            commentService.delete(Integer.parseInt(idsStr[i]));
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }
}
